//
//  UIView+Extension.m
//  JiLaiBao
//
//  Created by youmy on 15/8/14.
//  Copyright (c) 2015年 youmy. All rights reserved.
//

#import "UIView+Extension.h"

#define SEPARATE_LINE_DEFAULT_COLOR [UIColor colorWithHexString:@"#e8e8e8"]

CGFloat line_height = 0.33;

@implementation UIView (Extension)

- (void)setX:(CGFloat)x{
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
}

- (CGFloat)x{
    return self.frame.origin.x;
}

- (void)setY:(CGFloat)y{
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}

- (CGFloat)y{
    return self.frame.origin.y;
}

- (CGFloat)centerX{
    CGPoint center = self.center;
    return center.x;
}

- (void)setCenterX:(CGFloat)centerX{
    CGPoint center = self.center;
    center.x = centerX;
    self.center = center;
}

- (CGFloat)centerY{
    CGPoint center = self.center;
    return center.y;
}

- (void)setCenterY:(CGFloat)centerY{
    CGPoint center = self.center;
    center.y = centerY;
    self.center = center;
}

- (void)setWidth:(CGFloat)width{
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
}

- (CGFloat)width{
    return self.frame.size.width;
}

- (void)setHeight:(CGFloat)height{
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}

- (CGFloat)height{
    return self.frame.size.height;
}

- (void)setSize:(CGSize)size{
    CGRect frame = self.frame;
    frame.size = size;
    self.frame = frame;
}

- (CGSize)size{
    return self.frame.size;
}

- (void)setOrigin:(CGPoint)origin{
    CGRect frame = self.frame;
    frame.origin = origin;
    self.frame = frame;
}

- (CGPoint)origin{
    return self.frame.origin;
}

- (void)drawBorder{
    self.layer.borderColor = [UIColor colorWithWhite:0.7 alpha:0.5].CGColor;
    self.layer.borderWidth = line_height;
}

- (void)drawBorderWithColor:(UIColor *)color{
    self.layer.borderColor = color.CGColor;
    self.layer.borderWidth = line_height;
}

- (void)drawBorderWithColor:(UIColor *)color borderWidth:(CGFloat)width{
    self.layer.borderColor = color.CGColor;
    self.layer.borderWidth = width;
}

- (void)drawBorderWithCornerRadius:(CGFloat)radius{
    self.layer.cornerRadius = radius;
}

- (void)drawBorderWithColor:(UIColor *)color radius:(CGFloat)radius{
    self.layer.borderColor = color.CGColor;
    self.layer.borderWidth = line_height;
    self.layer.cornerRadius = radius;
}

- (void)drawBorderWithColor:(UIColor *)color borderWidth:(CGFloat)width radius:(CGFloat)radius{
    self.layer.borderColor = color.CGColor;
    self.layer.borderWidth = width;
    self.layer.cornerRadius = radius;
    [self clipsToBounds];
}

- (void)drawCircle:(CGFloat)witdh borderColor:(UIColor *)borderColor{
    UIBezierPath * maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:self.size];
    CAShapeLayer * maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;
    
    UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:50];
    CAShapeLayer * layer = [CAShapeLayer layer];
    layer.frame = self.bounds;
    layer.path = path.CGPath;
    layer.strokeColor = borderColor.CGColor;
    layer.fillColor = [UIColor clearColor].CGColor;
    layer.lineWidth = witdh;

    [self.layer addSublayer:layer];
}

- (void)drawBorder:(CGFloat)witdh borderColor:(UIColor *)borderColor{
    UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:self.width/2.0];
    CAShapeLayer * layer = [CAShapeLayer layer];
    layer.path = path.CGPath;
    layer.strokeColor = borderColor.CGColor;
    layer.lineWidth = witdh;
}


- (void)addShadow{
    self.layer.shadowColor = [UIColor redColor].CGColor;
    //    if (view == _SGUserView) {
    //        view.layer.shadowOffset = CGSizeMake(0, 0);
    //        view.layer.shadowOpacity = 0.7f;
    //        view.layer.shadowRadius = line_heightf;
    //        return;
    //    }
    self.layer.shadowOffset = CGSizeMake(10, 1);
    self.layer.shadowOpacity = 2;
    self.layer.shadowRadius = 2;
}

- (void)addBottomShadow:(UIColor *)color{
    self.layer.shadowColor = color.CGColor;
    self.layer.shadowOffset = CGSizeMake(0, 1);
    self.layer.shadowOpacity = 0.3;
    self.layer.shadowRadius = 5.0;
}

/** 画线 */
- (void)drawLineWithColor:(UIColor *)color rect:(CGRect)rect{
    [color set];
    UIBezierPath * path = [UIBezierPath bezierPath];
    [path moveToPoint:rect.origin];
    [path addLineToPoint:CGPointMake(rect.size.width, rect.size.height)];
    [path closePath];
    [path stroke];
    CAShapeLayer *shape = [[CAShapeLayer alloc] init];
    [shape setPath:path.CGPath];
    self.layer.mask = shape;
}

- (void)addTopLine{
    [self drawLineWithColor:SEPARATE_LINE_DEFAULT_COLOR rect:CGRectMake(0, 0, self.width, line_height)];
}

- (void)addTopLineWithLeft:(CGFloat)left right:(CGFloat)right{
    [self drawLineWithColor:SEPARATE_LINE_DEFAULT_COLOR rect:CGRectMake(left, 0, self.width - right, line_height)];
}

- (void)addTopLineWithColor:(UIColor *)color{
    [self drawLineWithColor:color rect:CGRectMake(0, 0, self.width, line_height)];
}

- (void)addTopLineWithColor:(UIColor *)color left:(CGFloat)left right:(CGFloat)right{
    [self drawLineWithColor:color rect:CGRectMake(left, 0, self.width - right, line_height)];
}

- (void)addTopLineWithColor:(UIColor *)color width:(CGFloat)width{
    [self drawLineWithColor:color rect:CGRectMake(0, 0, width, line_height)];
}

- (void)addBottomLine{
    [self drawLineWithColor:SEPARATE_LINE_DEFAULT_COLOR rect:CGRectMake(0, self.height - line_height, self.width, line_height)];
}

- (void)addBottomLineWithLeft:(CGFloat)left right:(CGFloat)right{
    [self drawLineWithColor:SEPARATE_LINE_DEFAULT_COLOR rect:CGRectMake(left, self.height - line_height, self.width - right - left, line_height)];
}

- (void)addBottomLineWithColor:(UIColor *)color left:(CGFloat)left right:(CGFloat)right{
    [self drawLineWithColor:color rect:CGRectMake(left, self.height - line_height, self.width - right, line_height)];
}

- (void)addBottomLineWithColor:(UIColor *)color{
    [self drawLineWithColor:color rect:CGRectMake(0, self.height - line_height, self.width, line_height)];
}

- (void)addBottomLineWithColor:(UIColor *)color height:(CGFloat)height{
    [self drawLineWithColor:color rect:CGRectMake(0, self.height - height, self.width, height)];
}

- (void)addLeftLine{
    [self drawLineWithColor:SEPARATE_LINE_DEFAULT_COLOR rect:CGRectMake(0, 0, line_height, self.height)];
}

- (void)addLeftLineWithColor:(UIColor *)color{
    [self drawLineWithColor:color rect:CGRectMake(0, 0, line_height, self.height)];
}

- (void)addLeftLineOffset:(CGFloat)offset{
    [self drawLineWithColor:SEPARATE_LINE_DEFAULT_COLOR rect:CGRectMake(offset, 0, line_height, self.height)];
}

- (void)addLeftLineWidth:(CGFloat)width{
    [self drawLineWithColor:SEPARATE_LINE_DEFAULT_COLOR rect:CGRectMake(0, 0, width, self.height)];
}

- (void)addLeftLineWidth:(CGFloat)width offset:(CGFloat)offset{
    [self drawLineWithColor:SEPARATE_LINE_DEFAULT_COLOR rect:CGRectMake(offset, 0, width, self.height)];
}

- (void)addLeftLineWithColor:(UIColor *)color width:(CGFloat)width{
    [self drawLineWithColor:color rect:CGRectMake(0, 0, width, self.height)];
}

- (void)addLeftLineWithColor:(UIColor *)color width:(CGFloat)width offset:(CGFloat)offset{
    [self drawLineWithColor:color rect:CGRectMake(offset, 0, width, self.height)];
}

- (void)addRightLine{
    [self drawLineWithColor:SEPARATE_LINE_DEFAULT_COLOR rect:CGRectMake(0, 0, line_height, self.height)];
}

- (void)addRightLineWithColor:(UIColor *)color{
    [self drawLineWithColor:color rect:CGRectMake(self.width - line_height, 0, line_height, self.height)];
}

- (void)addRightLineOffset:(CGFloat)offset{
    [self drawLineWithColor:SEPARATE_LINE_DEFAULT_COLOR rect:CGRectMake(self.width - line_height - offset, 0, line_height, self.height)];
}

- (void)addRightLineWidth:(CGFloat)width{
    [self drawLineWithColor:SEPARATE_LINE_DEFAULT_COLOR rect:CGRectMake(self.width - width, 0, width, self.height)];
}

- (void)addRightLineWiidth:(CGFloat)width offset:(CGFloat)offset{
    [self drawLineWithColor:SEPARATE_LINE_DEFAULT_COLOR rect:CGRectMake(self.width - width - offset, 0, width, self.height)];
}

- (void)addRightLineWithColor:(UIColor *)color width:(CGFloat)width{
    [self drawLineWithColor:color rect:CGRectMake(self.width - width, 0, width, self.height)];
}

- (void)addRightLineWithColor:(UIColor *)color width:(CGFloat)width offset:(CGFloat)offset{
    [self drawLineWithColor:color rect:CGRectMake(self.width - width - offset, 0, width, self.height)];
}

- (void)hideKeyboard{
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
}

- (void)allIvar{
    uint outCount;
    
    Ivar * ivars = class_copyIvarList([self class], &outCount);
    
    for (int i = 0; i < outCount; i++) {
        Ivar ivar = ivars[i];
        NSLog(@"---Ivar-->%@",[NSString stringWithUTF8String:ivar_getName(ivar)]);
    }
}

- (void)allProperty{
    unsigned int outCount;
    int i;
    objc_property_t * pProperty = class_copyPropertyList([self class], &outCount);
    for (i = outCount - 1; i >= 0; i--) {
        NSString * propertyName = [NSString stringWithCString:property_getName(pProperty[i]) encoding:NSUTF8StringEncoding];
        NSString * getPropertyNameString = [NSString stringWithCString:property_getAttributes(pProperty[i]) encoding:NSUTF8StringEncoding];
        NSLog(@"---Property---%@--->%@",getPropertyNameString,propertyName);
    }
}
- (UIView *) getFirstResponder
{
    if (self.isFirstResponder) {
        return self;
    }
    
    for (UIView *subView in self.subviews) {
        UIView *firstResponder = [subView getFirstResponder];
        if (firstResponder != nil) {
            return firstResponder;
        }
    }
    
    return nil;
}

+(UIView *)sectionHeader{
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
    view.backgroundColor = CONTENT_BG_COLOR;
    
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, SCREEN_WIDTH - 15, view.height)];
    label.text = @"支付方式";
    label.textColor = SUB_TITLE_COLOR;
    label.font = [UIFont systemFontOfSize:15];
    [view addSubview:label];
    return view;
}
/**
 *  设置部分圆角(绝对布局)
 *
 *  @param corners 需要设置为圆角的角 UIRectCornerTopLeft | UIRectCornerTopRight | UIRectCornerBottomLeft | UIRectCornerBottomRight | UIRectCornerAllCorners
 *  @param radii   需要设置的圆角大小 例如 CGSizeMake(20.0f, 20.0f)
 */
- (void)addRoundedCorners:(UIRectCorner)corners
                withRadii:(CGSize)radii {
    
    UIBezierPath* rounded = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:radii];
    CAShapeLayer* shape = [[CAShapeLayer alloc] init];
    [shape setPath:rounded.CGPath];
    
    self.layer.mask = shape;
}

/**
 *  设置部分圆角(相对布局)
 *
 *  @param corners 需要设置为圆角的角 UIRectCornerTopLeft | UIRectCornerTopRight | UIRectCornerBottomLeft | UIRectCornerBottomRight | UIRectCornerAllCorners
 *  @param radii   需要设置的圆角大小 例如 CGSizeMake(20.0f, 20.0f)
 *  @param rect    需要设置的圆角view的rect
 */
- (void)addRoundedCorners:(UIRectCorner)corners
                withRadii:(CGSize)radii
                 viewRect:(CGRect)rect {
    
    UIBezierPath* rounded = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corners cornerRadii:radii];
    CAShapeLayer* shape = [[CAShapeLayer alloc] init];
    [shape setPath:rounded.CGPath];
    
    self.layer.mask = shape;
}
@end
